﻿package com.projects.xenia;

import java.util.Calendar;
import java.util.Date;

import com.projects.xenia.global_modules.MainGlobalModule;

import android.R;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.location.LocationManager;

import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

public class Documents_Location {

	private LocationManager MyGPSLocation;
	private Activity ParentActivity;
	
	private Double LocationLongtitude;
	private Double LocationLattitude;
	private Float LocationAccuracy;
	private Date LocationTime;
		
	private int ActionType;
	private String ActionId;
	
	private ImageView StateImage;
	
	public enum GPSStateEnum {none,approximate,accurate}
	
	public GPSStateEnum GetGPSState (String lActionId) {
		
		Cursor LocationData = MainGlobalModule.DBE.query("xdoc_gpslog", new String [] {"Accuracy"}, "id=?", new String [] {lActionId}, null, null, null);
		if (LocationData.getCount()>0) {
			LocationData.moveToFirst();
			if (LocationData.getFloat(0)>40) {
				return (GPSStateEnum.approximate);
			}
			else
			{
				return (GPSStateEnum.accurate);
			}
		}
			return (GPSStateEnum.none);		
	}
	
	private void GetGpsLog () {

		Cursor LocationData = MainGlobalModule.DBE.query("xdoc_gpslog", new String [] {"Longtitude","Lattitude","Accuracy","LocationTime"}, "id=?", new String [] {ActionId}, null, null, null);
		
		if (LocationData.getCount()>0) {
			LocationData.moveToFirst();
			LocationLongtitude = LocationData.getDouble(0); 
			LocationLattitude = LocationData.getDouble(1);
			LocationAccuracy = LocationData.getFloat(2);
			LocationTime =  MainGlobalModule.SqlStringToDate(LocationData.getString (3));
			
			StateImage.post(new Runnable() {						
				@Override
				public void run() {
					StateImage.setVisibility(View.VISIBLE);						 
					StateImage.setImageResource(LocationAccuracy<40d?R.drawable.btn_star_big_on:R.drawable.btn_star_big_off);
					}						
				}
			);
			
		}			
		else {
			LocationLongtitude = 0.0d; 
			LocationLattitude = 0.0d;
			LocationAccuracy = 999999f;
			LocationTime = null;
		}
		
	}
	
	public void StartLocation (Activity lParentActivity,int lActionType, String lActionId, ImageView lStateImage) {
	
		StateImage = lStateImage;
		ParentActivity = lParentActivity;
		ActionType = lActionType;
		ActionId = lActionId;	
		
		GetGpsLog ();
		
		try {
			if (LocationTime == null) {
				
				MyGPSLocation = (LocationManager)ParentActivity.getSystemService(ParentActivity.LOCATION_SERVICE);
				//try {
				MyGPSLocation.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, lLocationListener);	
				MyGPSLocation.requestLocationUpdates(LocationManager.PASSIVE_PROVIDER, 0, 0, lLocationListener);
				//} catch (Exception e) {
				// TODO: handle exception
				//}
				
				//try {
				MyGPSLocation.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, lLocationListener);	
				//} catch (Exception e) {
				// TODO: handle exception
				//}
					
			}
			
		} catch (Exception e) {
			// TODO: handle exception
		}
			
	}
	
	public void StopLocation () {
		
		if (MyGPSLocation!=null) {
			MyGPSLocation.removeUpdates(lLocationListener);
		}
		
	}
	
	private LocationListener lLocationListener = new LocationListener() {
		
		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderEnabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onProviderDisabled(String provider) {
			// TODO Auto-generated method stub
			
		}
		
		@Override
		public void onLocationChanged(Location location) {			
			
			if (LocationAccuracy > location.getAccuracy()){
				
				LocationLongtitude = location.getLongitude();
				LocationLattitude = location.getLatitude();
				LocationAccuracy = location.getAccuracy();
				LocationTime = new Date (location.getTime());
				
				ContentValues InsertUpdateValues = new ContentValues ();
				
				InsertUpdateValues.put("id", ActionId);
				InsertUpdateValues.put("type_xdoc", ActionType);				
				InsertUpdateValues.put("date_create", MainGlobalModule.DateToSqlString(Calendar.getInstance().getTime()));
				InsertUpdateValues.put("state", 1);
				InsertUpdateValues.put("user_id", MainGlobalModule.SettingsCollection.get("ManagerId"));
				
				InsertUpdateValues.put("longtitude", LocationLongtitude);
				InsertUpdateValues.put("lattitude", LocationLattitude);
				InsertUpdateValues.put("accuracy", LocationAccuracy);
				InsertUpdateValues.put("locationTime", MainGlobalModule.DateToSqlString(LocationTime));
				
				if (MainGlobalModule.DBE.insert("xdoc_gpslog", null, InsertUpdateValues)<0) {
					MainGlobalModule.DBE.update("xdoc_gpslog",InsertUpdateValues,"id=?",new String [] {ActionId});
				}
				
				
				StateImage.post(new Runnable() {						
					@Override
					public void run() {
						StateImage.setVisibility(View.VISIBLE);						 
						StateImage.setImageResource(LocationAccuracy<40d?R.drawable.btn_star_big_on:R.drawable.btn_star_big_off);
						}						
					}
				);
				
				if (LocationAccuracy<40d) {
					MyGPSLocation.removeUpdates(lLocationListener);	
				}
				
				
			}			
		}
	
	}; 
	
	
	
}
